package com.yijia.work4.question5.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

public class Question5Filter implements Filter {

//    private FilterConfig config = null;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
//        this.config = filterConfig;
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        PrintWriter out = servletResponse.getWriter();
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        //对request和response进行一些预处理
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;character=UTF-8");

        Date date = new Date();
        Long t1 = date.getTime();

        filterChain.doFilter(request, response);

        Long t2 = date.getTime();

        out.print("执行本页面共花费时间:" + (t2 - t1) + "毫秒");

    }

    @Override
    public void destroy() {
//        config = null;
    }
}
